#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "601b";
const char* password = "4001000601b";

const char* host = "192.168.0.180"; //需要访问的域名
const int httpsPort = 4500;  // 需要访问的端口
const String url = "/input";   // 需要访问的地址

int DSPIN = D4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password); // 连接WIFI
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP()); // 显示WIFI地址
}
 
void loop() {
  
  Serial.print("connecting to ");  
  Serial.println(host);  

  WiFiClient client;  

  /**
   * 测试是否正常连接
   */
  if (!client.connect(host, httpsPort)) {  
    Serial.println("connection failed");  
    return;  
  }  
  delay(10);  
  
  // put your main code here, to run repeatedly:
  double temp = TempRead();
  temp *= 0.0625;//转换精度为0.0625/LSB
  Serial.println(temp);
  delay(500);

  
  String postRequest =(String)("GET ") + url+"?key=temp&value="+ temp  + "/ HTTP/1.1\r\n" +  
    "Content-Type: text/html;charset=utf-8\r\n" +  
    "Host: " + host + "\r\n" + 
    "User-Agent: BuildFailureDetectorESP8266\r\n" +
    "Connection: Keep Alive\r\n\r\n";  
  Serial.println(postRequest);  
  client.print(postRequest);  // 发送HTTP请求

  /**
   * 展示返回的所有信息
   */
  String line = client.readStringUntil('\n');
  while(line.length() != 0){
    Serial.println(line);
    line = client.readStringUntil('\n');
  }
  Serial.println(line);
  client.stop();
  delay(3000); 
  
}
 
boolean DS18B20_Init()
{
  pinMode(DSPIN, OUTPUT);
  digitalWrite(DSPIN, HIGH);
  delayMicroseconds(5);
  digitalWrite(DSPIN, LOW);
  delayMicroseconds(750);//480-960
  digitalWrite(DSPIN, HIGH);
  pinMode(DSPIN, INPUT);
  int t = 0;
  while(digitalRead(DSPIN))
  {
    t++;
    if(t > 60) return false;
    delayMicroseconds(1);
  }
  t = 480 - t;
  pinMode(DSPIN, OUTPUT);
  delayMicroseconds(t);
  digitalWrite(DSPIN, HIGH);
  return true;
}
 
void DS18B20_Write(byte data)
{
  pinMode(DSPIN, OUTPUT);
  for(int i=0; i<8; i++)
  {
    digitalWrite(DSPIN, LOW);
    delayMicroseconds(10);
    if(data & 1) digitalWrite(DSPIN, HIGH);
    else digitalWrite(DSPIN, LOW);
    data >>= 1;
    delayMicroseconds(50);
    digitalWrite(DSPIN, HIGH);
  }
}
 
byte DS18B20_Read()
{
  pinMode(DSPIN, OUTPUT);
  digitalWrite(DSPIN, HIGH);
  delayMicroseconds(2);
  byte data = 0;
  for(int i=0; i<8; i++)
  {
    digitalWrite(DSPIN, LOW);
    delayMicroseconds(1);
    digitalWrite(DSPIN, HIGH);
    pinMode(DSPIN, INPUT);
    delayMicroseconds(5);
    data >>= 1;
    if(digitalRead(DSPIN)) data |= 0x80;
    delayMicroseconds(55);
    pinMode(DSPIN, OUTPUT);
    digitalWrite(DSPIN, HIGH);
  }
  return data;
}
 
int TempRead()
{
  if(!DS18B20_Init()) return 0;
  DS18B20_Write(0xCC);//发跳过ROM命令
  DS18B20_Write(0x44);//发读开始转换命令
  if(!DS18B20_Init()) return 0;
  DS18B20_Write(0xCC);//发跳过ROM命令
  DS18B20_Write(0xBE);//读寄存器，共九字节，前两字节为转换值
  int temp = DS18B20_Read();//低字节
  temp |= DS18B20_Read() << 8;//高字节
  return temp;
}
